class Solution:
    def removeDuplicateLetters(self, s: str) -> str:
        """
        """
        last_index = [0] * 26
        for i in range(len(s)):
            last_index[ord(s[i]) - 97] = i

        visited = [0] * 26
        stack = []
        for i in range(len(s)):
            if visited[ord(s[i]) - 97]:
                continue
            while stack and s[i] < stack[-1] and last_index[ord(stack[-1]) - 97] > i:
                top = stack.pop()
                visited[ord(top) - 97] = 0

            stack.append(s[i])
            visited[ord(s[i]) - 97] = 1
            print(i, stack)
        return "".join(stack)
